package com.example.blog.controller;

import com.example.blog.enums.ResultEnums;
import com.example.blog.model.BlogInfo;
import com.example.blog.model.Result;
import com.example.blog.service.BlogService;
import com.example.blog.util.JWTutil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogInfoController {
    @Autowired
    private BlogService blogService;

    @RequestMapping("/getList")
    public List<BlogInfo> getList(){
        log.info("获取所有博客");
        List<BlogInfo> list = blogService.selectAll();
        return list;
    }

    @RequestMapping("/getBlogDetail")
    public Result getBlogDetail(Integer blogId, HttpServletRequest request){
        log.info("输入id:{},获取指定博客",blogId);
        Assert.notNull(blogId,"输入ID不能为空");
        BlogInfo blogInfo = blogService.selectById(blogId);
        if(blogInfo==null){
            log.error("没有找到该博客");
            return Result.fail(ResultEnums.NOT_FIND.getCode(),"没有该博客");
        }
        //获取作者id
        Integer authorId = blogInfo.getUserId();
        //登录信息
        String token = request.getHeader("user_token");
        Integer userId = JWTutil.getUserIdByToken(token);
        if(authorId!=null&&authorId==userId){
            //就是本人
            blogInfo.setLoginUser(true);
        }
        return Result.success(blogInfo);
    }

    @RequestMapping("/delete")
    public Boolean deleteBlog(Integer blogId){
        log.info("输入id:{},删除指定博客",blogId);
        Assert.notNull(blogId,"输入ID不能为空");
        Integer result = blogService.delete(blogId);
        try {
            return result>0;
        }catch (Exception e){
            log.error("没有信息");
            return false;
        }
    }

    //添加
    @RequestMapping("/add")
    public Boolean addInfo(@Validated @RequestBody BlogInfo blogInfo, HttpServletRequest request){
        log.info("添加新博客,BlogInfo:{}",blogInfo);
        //获取userId
        String token = request.getHeader("user_token");
        Integer userId = JWTutil.getUserIdByToken(token);
        blogInfo.setUserId(userId);
        Integer result = blogService.insert(blogInfo);
        try {
            return result>0;
        }catch (Exception e){
            log.error("添加失败,e:{}",e.getMessage());
            return false;
        }
    }

    //更新
    @RequestMapping("/update")
    public Boolean updateInfo(@Validated@RequestBody BlogInfo blogInfo){
        log.info("update博客,修改后,BlogInfo:{}",blogInfo);
        Integer result = blogService.update(blogInfo);
        try {
            return result>0;
        }catch (Exception e){
            log.error("修改失败");
            return false;
        }

    }
}
